package com.springBoot.service.impl;

import com.alibaba.dashscope.aigc.generation.Generation;
import com.alibaba.dashscope.aigc.generation.GenerationParam;
import com.alibaba.dashscope.aigc.multimodalconversation.MultiModalConversation;
import com.alibaba.dashscope.aigc.multimodalconversation.MultiModalConversationParam;
import com.alibaba.dashscope.aigc.multimodalconversation.MultiModalConversationResult;
import com.alibaba.dashscope.common.Message;
import com.alibaba.dashscope.common.MultiModalMessage;
import com.alibaba.dashscope.common.Role;
import com.alibaba.dashscope.exception.InputRequiredException;
import com.alibaba.dashscope.exception.NoApiKeyException;
import com.alibaba.dashscope.exception.UploadFileException;
import com.springBoot.service.ReadPic;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.*;

@Service
public class ReadPicImpl implements ReadPic {
    @Override
    public void readPicByAi(ArrayList<String> picList) {
        MultiModalConversation conv = new MultiModalConversation();
        String imageReply = "";
        MultiModalMessage systemMessage = MultiModalMessage.builder().role(Role.SYSTEM.getValue())
                .content(List.of(
                        Collections.singletonMap("text", "你是一名小助手."))).build();

        List<Map<String, Object>> contentList = new ArrayList<>();

        for (String pic : picList) {
            System.out.println("pic:"+pic);
            if (isLocalFile(pic)){
                contentList.add(new HashMap<String,Object>(){{put("image",pic);}});
            }else {
                contentList.add(Collections.singletonMap("image", pic));
            }

        }

        contentList.add(Collections.singletonMap("text", "请简单描述这些图片内容"));
        MultiModalMessage userMessage = MultiModalMessage.builder()
                .role(Role.USER.getValue())
                .content(contentList)
                .build();
        MultiModalConversationParam param = MultiModalConversationParam.builder()
                .apiKey("sk-6cba940395fa43138c78a46d32cd7488")
                .model("qwen-vl-max-latest")
                .messages(Arrays.asList(systemMessage, userMessage))
                .build();
        try {
            MultiModalConversationResult result = conv.call(param);
//            System.out.println("image content::==");
            System.out.println((String) result.getOutput().getChoices().get(0).getMessage().getContent().get(0).get("text"));
//            System.out.println("ending");
        } catch (NoApiKeyException | UploadFileException e) {
            throw new RuntimeException("处理图片时出错", e);
        }
    }

    @Override
    public void processText(String text) {
        String inputText = text + " 请你总结这段文字。";
        Generation gen = new Generation();
        Message systemMsg = Message.builder()
                .role(Role.SYSTEM.getValue())
                .content("You are a helpful assistant.")
                .build();
        Message userMsg = Message.builder()
                .role(Role.USER.getValue())
                .content(inputText)
                .build();
        GenerationParam param = GenerationParam.builder()
                .apiKey("sk-6cba940395fa43138c78a46d32cd7488")
                .model("qwen-plus")
                .messages(Arrays.asList(systemMsg, userMsg))
                .resultFormat(GenerationParam.ResultFormat.MESSAGE)
                .build();
        try {
//            System.out.println("text starting====");
            System.out.println(gen.call(param).getOutput().getChoices().get(0).getMessage().getContent());
//            System.out.println("text process ending====");
        } catch (NoApiKeyException e) {
            throw new RuntimeException(e);
        } catch (InputRequiredException e) {
            throw new RuntimeException(e);
        }
    }


    private boolean isLocalFile(String path) {
        return path.startsWith("/") ||
                path.matches("[a-zA-Z]:\\\\.*") ||
                path.startsWith("D:/")||
                path.startsWith("C:/");
    }
}
